package com.bw.module_device.viewmodel

import androidx.lifecycle.MutableLiveData
import com.bw.lib_common.coroutine.CoroutineHelper
import com.bw.lib_common.vm.BaseViewModel2
import com.bw.module_device.entity.DeviceResult
import com.bw.module_device.repo.DeviceRepo
import kotlinx.coroutines.launch
/**
 * 智慧园区-01-设备管理-v1.1-吴熙-20250507
 */
class DeviceViewModel: BaseViewModel2<DeviceRepo>() {

    val getDeviceListSuccess by lazy { MutableLiveData<List<DeviceResult>>() }
    val getDeviceDetailSuccess by lazy { MutableLiveData<DeviceResult>() }
    val addDeviceSuccess by lazy { MutableLiveData<DeviceResult>() }
    val deleteDeviceSuccess by lazy { MutableLiveData<String>() }

    fun getDeviceList(){
        CoroutineHelper.mViewModelScope.launch(CoroutineHelper.mDispatcher){
            val result = mRepository.getDeviceList()
            if (result.code == 200){
                getDeviceListSuccess.postValue(result.data)
            } else {
                mErrorLiveData.postValue("获取数据失败！！")
            }
        }
    }

    fun getDeviceDetail(deviceId: Int){
        CoroutineHelper.mViewModelScope.launch(CoroutineHelper.mDispatcher){
            val result = mRepository.getDeviceDetail(deviceId)
            if (result.code == 200){
                getDeviceDetailSuccess.postValue(result.data)
            } else {
                mErrorLiveData.postValue("获取数据失败！！")
            }
        }
    }

    fun addDevice(deviceResult: DeviceResult){
        CoroutineHelper.mViewModelScope.launch(CoroutineHelper.mDispatcher){
            val result = mRepository.addDevice(deviceResult)
            if (result.code == 200){
                addDeviceSuccess.postValue(result.data)
            } else {
                mErrorLiveData.postValue("获取数据失败！！")
            }
        }
    }

    fun deleteDevice(deviceId: Int){
        CoroutineHelper.mViewModelScope.launch(CoroutineHelper.mDispatcher){
            val result = mRepository.deleteDevice(deviceId)
            if (result.code == 200){
                deleteDeviceSuccess.postValue(result.message)
            } else {
                mErrorLiveData.postValue("获取数据失败！！")
            }
        }
    }

    override fun createRepository(): DeviceRepo = DeviceRepo()

}